﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using VH.SharePoint.Api.MetaWeblog;
using VH.SharePoint.Api.Queries;

namespace VH.SharePoint.Api.Mock
{
    public class MockPostRepository : PostRepository
    {
        private static readonly List<Post> _storage = new List<Post>();

        static MockPostRepository()
        {
            _storage.Add(new Post
                {
                    Id = 0,
                    Title = "Welcome to your Blog!",
                    Description = "<div class=\"ExternalClass5B3C42AFF7194A12AC1C28D2CC58A1EA\"><h1>Try using heading1</h1><p><span style=\"font-family:Times New Roman;font-size:12pt\">To begin using your site, click <strong>Create a Post</strong> under <strong>Blog Tools</strong>.</span></p><p><img src=\"/blog/Lists/Photos/050113_1338_Welcometoyo1.png\" alt=\"\"><span style=\"font-family:Times New Roman;font-size:12pt\"><strong>​</strong></span></p><p><img src=\"/blog/Lists/Photos/050113_1338_Welcometoyo2.png\" alt=\"\"><span style=\"font-family:Times New Roman;font-size:12pt\"></span></p><p><span style=\"font-family:Times New Roman;font-size:12pt\"><strong>What is a Blog?</strong></span></p><p><span style=\"font-family:Times New Roman;font-size:12pt\">A Blog is a Web site designed to help you share information related to a particular subject area in the form of text, images, links, and other media such as video. Blogs can be used as team sites, news sites, journals, diaries, and more.</span></p><p><span style=\"font-family:Times New Roman;font-size:12pt\">Blog posts usually consist of frequent short postings and are typically displayed in reverse chronological order (newest entries first). Blogs encourage site visitors to interact with one another by leaving comments on posts.</span></p><p><span style=\"font-family:Times New Roman;font-size:12pt\">Blogs can be also be used as a team communication tool. Keep team members informed by providing a central place for links and relevant news.</span></p><p>&nbsp;&nbsp;</p><p><span style=\"font-family:Times New Roman;font-size:12pt\">​</span></p><p>​​</p></div>",
                    Created = DateTime.Now.AddDays(-10),
                    Modified = DateTime.Now.AddDays(-2)
                });
        }

        public override IEnumerable<Post> FindAll()
        {
            return _storage.ToList();
        }

        public override IEnumerable<Post> FindAll(QueryConstraints<Post> constraints)
        {
            return constraints.ApplyTo(_storage.AsQueryable()).ToList();
        }

        public override Post Get(string id)
        {
            var i = int.Parse(id);
            return _storage.First(p => p.Id == i);
        }

        public override void Create(Post entity)
        {
            entity.Id = _storage.Count;
            entity.Created = DateTime.Now;
            _storage.Add(entity);
        }

        public override void Update(Post entity)
        {
            var post = Get(entity.Id.ToString(CultureInfo.InvariantCulture));
            post.Modified = DateTime.Now;
            post.Title = entity.Title;
            post.Description = entity.Description;
            post.Published = entity.Published;
            post.Url = entity.Url;
        }

        public override void Delete(Post entity)
        {
            _storage.Remove(entity);
        }

        public override void ValidateUser(string username, string password)
        {
            
        }
    }
}
